﻿Imports System.Windows.Forms

Public Class frmCSection

    Dim pForm As frmMain

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        Select Case cCopyNumber.Value

            Case 1
                'MsgBox("a")

                If txtCopy.Text <> txtOrg.Text Then
                    Dim orgLocation As Integer = pForm.lstSection.SelectedIndex + 1

                    pForm.iSection = cSection.InsertSection(pForm.iSection, orgLocation, txtCopy.Text)
                    Dim tmpStr1 As String = pForm.iSection(cSection.GetSectionIndexFormName(pForm.iSection, txtOrg.Text)).SectionContext
                    pForm.iSection(orgLocation).SectionContext = Replace(tmpStr1, "[" + txtOrg.Text + "]", "[" + txtCopy.Text + "]", 1, 1, CompareMethod.Text)

                    If chkAReg.Checked = True Then
                        cReg.GeneralRegNewUnit(pForm, txtCopy.Text, cboSection.Text)
                    End If

                    pForm.lstSection.Items.Clear()
                    For Each root As KeyValue.INISection In pForm.iSection
                        If root.SectionName <> Nothing Then pForm.lstSection.Items.Add(root.SectionName)
                    Next

                    pForm.lstSection.SelectedIndex = orgLocation
                End If

            Case Is > 1

                For i = 1 To cCopyNumber.Value

                    Dim orgLocation As Integer = pForm.lstSection.SelectedIndex + 1
                    Dim regN As String = txtCopy.Text + CStr(i)

                    pForm.iSection = cSection.InsertSection(pForm.iSection, orgLocation, regN)
                    Dim tmpStr1 As String = pForm.iSection(cSection.GetSectionIndexFormName(pForm.iSection, txtOrg.Text)).SectionContext
                    pForm.iSection(orgLocation).SectionContext = Replace(tmpStr1, "[" + txtOrg.Text + "]", "[" + regN + "]", 1, 1, CompareMethod.Text)

                    If chkAReg.Checked = True Then
                        cReg.GeneralRegNewUnit(pForm, regN, cboSection.Text)
                    End If

                    pForm.lstSection.SelectedIndex = orgLocation
                Next

                pForm.lstSection.Items.Clear()
                pForm.ccdFunctions = ""
                For Each root As KeyValue.INISection In pForm.iSection
                    If root.SectionName <> Nothing Then pForm.lstSection.Items.Add(root.SectionName)

                    If pForm.ccdFunctions = "" Then
                        pForm.ccdFunctions = root.SectionName
                    Else
                        pForm.ccdFunctions = pForm.ccdFunctions + "," + root.SectionName
                    End If

                Next

        End Select

        pForm.UpdateObjectListArray()
        'Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        'Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmCSection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        pForm = DirectCast(frmMDIParent.ActiveMdiChild, frmMain)
        Me.TopMost = True
        cmdLoad_Click()
    End Sub

    Private Sub cmdLoad_Click() Handles cmdLoad.Click
        txtOrg.Text = Trim(pForm.lstSection.SelectedItem)
        txtCopy.Text = txtOrg.Text & "2"
    End Sub

    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click

    End Sub
End Class
